package com.lmx.videoplay.image;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.lmx.videoplay.AppDataSourse;
import com.lmx.videoplay.R;
import com.lmx.videoplay.library.SlidingRecyclerView;
import com.lmx.videoplay.util.Constant;
import com.lmx.videoplay.util.DisplayUtil;
import com.lmx.videoplay.util.XUtilsManager;
import java.io.IOException;

import cc.shinichi.wallpaperlib.SetWallpaper;

/**
 * Author : Z-JC
 * Date : 2020/3/6
 * Description :
 */
public class ImageDetailsActivity extends Activity implements View.OnClickListener, ImageDetailsAdapter.OnPageSelectedLister {

    private String TAG = getClass().getSimpleName();
    private SlidingRecyclerView rvImage;
    private ImageButton ibBack;
    private ImageDetailsAdapter adapter;
    private Button btn1;
    private Button btn2;
    private WallpaperManager wallpaperManager;
    private String imgPath;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_details);
        initView();
    }

    private void initView() {
        wallpaperManager = WallpaperManager.getInstance(this);
        rvImage = (SlidingRecyclerView) findViewById(R.id.rvImage);
        ibBack = (ImageButton) findViewById(R.id.ibBack);
        ibBack.setOnClickListener(this);

        adapter = new ImageDetailsAdapter(this, this);
        rvImage.setAdapter(adapter);
        adapter.setData(AppDataSourse.getImageList());
        btn1 = (Button) findViewById(R.id.btn_1);
        btn2 = (Button) findViewById(R.id.btn_2);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
    }

    /**
     * url转bitmap
     *
     * @param imgUrl  网络图片URL
     * @param context
     */
    public void initSystem(final String imgUrl, final Activity context) {
        new AsyncTask<Void, Void, Bitmap>() {
            @Override
            protected Bitmap doInBackground(Void... params) {
                Bitmap bitmap = null;
                try {
                    bitmap = Glide.with(context)
                            .asBitmap()
                            .load(imgUrl)
                            .submit(DisplayUtil.getScreenWidth(ImageDetailsActivity.this), DisplayUtil.getScreenHeight(ImageDetailsActivity.this))
                            .get();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return bitmap;
            }

            @Override
            protected void onPostExecute(Bitmap bitmap) {
                Log.e(TAG, "图片压缩成功:" + bitmap.getWidth() + "*" + bitmap.getHeight());
                try {
                    wallpaperManager.setBitmap(bitmap);
                    Toast.makeText(ImageDetailsActivity.this, "设置成功", Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    e.printStackTrace();
                    Toast.makeText(ImageDetailsActivity.this, "设置失败", Toast.LENGTH_SHORT).show();
                    Log.e(TAG, "设置壁纸异常:" + e.toString());
                }
            }
        }.execute();
    }

    @SuppressLint({"ResourceType", "NewApi"})
    @Override
    public void onClick(View v) {
        Log.e(TAG, "当前图片地址:" + imgPath);
        switch (v.getId()) {
            case R.id.ibBack:
                finish();
                break;
            case R.id.btn_1://设置桌面壁纸
                initSystem(imgPath, ImageDetailsActivity.this);
                break;
            case R.id.btn_2:
                XUtilsManager.getInstance().getDownLoad(200, new XUtilsManager.HttpCallBack() {
                    @Override
                    public void onEndDownload(int what, String end) {

                    }

                    @Override
                    public void onStartDownload(int what, String start) {

                    }

                    @Override
                    public void onSchedule(int what, int schedule) {

                    }

                    @Override
                    public void onResponse(int what, String response) {
                        Toast.makeText(ImageDetailsActivity.this, "下载成功", Toast.LENGTH_SHORT).show();
                        SetWallpaper.setWallpaper(ImageDetailsActivity.this, response, "com.lmx.videoplay.fileprovider");// authority（7.0 文件共享权限）
                    }

                    @Override
                    public void onFailure(int what, String error) {

                    }
                }, Constant.getDCIM(), imgPath);
                break;
        }
    }

    @Override
    public void onPageSelected(int position) {
        imgPath = adapter.imgList.get(position);
    }
}
